home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-05-21 | 4.4 KB | 202 lines | [TEXT/ttxt] |
- --<<<
-
- in module WebImplementation
-
- -- Simple implementation of the HTTP protocol
-
- global endOfLine := new String
- append endOfLine 13
- append endOfLine 10
-
- function getline stream -> (
- local c
- local line := new String
-
- repeat until ((c := read stream) == 13) or (c == 10 )do append line c
- if (c == 13) do
- read stream
-
- return line
- )
-
- function readAll s #key target: (new String) -> (
- repeat until (isPastEnd s) do append target (read s)
- target
- )
-
- function readSome s #key target: (new String) -> (
- for i := 1 to (readReady s) do append target (read s)
- target
- )
-
-
- -- Connect to the server and request the specified document.
- -- Return a stream from which the data get can be read.
-
- function gethttp url -> (
- local file := url.path
- if file = undefined do file := "/"
-
- local hostmachine := url.domainName
- local port := url.port
- -- local starttime := theeventtimestampclock.time
- local s := new tcpstream host: hostmachine \
- port: (if (port == undefined) then 80 else port)
- -- local gotstreamtime := theeventtimestampclock.time
- local line
- local code
- local version := "HTTP/1.0" as String
- local n
- local headers := new hashtable
-
- local req :=
- "GET " \
- + file \
- + " " \
- + version \
- + endOfLine \
- + "ACCEPT: */*" \
- + endofLine \
- + endOfLine
-
- writestring s req
-
- line := getLine s
-
- -- local gotresponsetime := theeventtimestampclock.time
-
- -- Check that the first three characters of this line
- -- are a successful response code
-
- if (copyfromTo line 0 (size version)) != version do (
- report (new generalexception name: "Bad protocol version") line
- )
-
- n := size(version) + 1;
- code := (copyfromto line n (3 + n)) as Integer
-
- if code != 200 do (
- -- This is roundabout
- report (new generalexception name: "Bad response code") line
- )
-
- -- Process the headers
- -- until blankline
- -- Does not deal with multiline headers
-
- repeat until (line := getLine(s)) = "" do (
- local pos := getOrdOne line (":"[1])
- if (pos > 0) do (
- local name := getlowercase (copyFromTo line 0 (pos - 1))
- local value := copyFromTo line (1 + pos) (size line)
- headers[name] := value
- )
- );
-
- -- local doneheaderstime := theeventtimestampclock.time
-
- /*
- format debug "Stream time = %*\n" \
- (((gotstreamtime - starttime) as integer) / starttime.scale)
-
- format debug "Response time = %*\n" \
- (((gotresponsetime - gotstreamtime) as integer) / starttime.scale)
-
- format debug "headers time = %*\n" \
- (((doneheaderstime - gotresponsetime) as integer) / starttime.scale)
-
- */
-
- #(headers, s)
- )
-
- function posthttp url data #rest args -> (
- local file := url.path
- if file = undefined do file := "/"
-
- local hostmachine := url.domainName
- local port := url.port
- local s := new tcpstream host: hostmachine \
- port: (if (port == undefined) then 80 else port)
- local line
- local code
- local version := "HTTP/1.0" as String
- local n
- local headers := new hashtable
-
- print "sending"
-
- local req := (
- "POST " +
- file +
- " " +
- version +
- endOfLine +
- "ACCEPT: */*" +
- endofLine +
- "content-length: " +
- ((size data) as String) +
- endofLine +
- "content-type: application/x-www-urlencoded" +
- endofLine +
- endOfLine +
- data)
-
- writestring s req
-
- print "getting"
-
- line := getLine s
-
- -- local gotresponsetime := theeventtimestampclock.time
-
- -- Check that the first three characters of this line
- -- are a successful response code
-
- if (copyfromTo line 0 (size version)) != version do (
- report (new generalexception name: "Bad protocol version") line
- )
-
- n := size(version) + 1;
- code := (copyfromto line n (3 + n)) as Integer
-
- if code != 200 do (
- -- This is roundabout
- report (new generalexception name: "Bad response code") line
- )
-
- -- Process the headers
- -- until blankline
- -- Does not deal with multiline headers
-
- repeat until (line := getLine(s)) = "" do (
- local pos := getOrdOne line (":"[1])
- if (pos > 0) do (
- local name := getlowercase (copyFromTo line 0 (pos - 1))
- local value := copyFromTo line (1 + pos) (size line)
- headers[name] := value
- )
- );
-
- -- local doneheaderstime := theeventtimestampclock.time
-
- /*
- format debug "Stream time = %*\n" \
- (((gotstreamtime - starttime) as integer) / starttime.scale)
-
- format debug "Response time = %*\n" \
- (((gotresponsetime - gotstreamtime) as integer) / starttime.scale)
-
- format debug "headers time = %*\n" \
- (((doneheaderstime - gotresponsetime) as integer) / starttime.scale)
-
- */
-
- #(headers, s)
- )
-
- registerAccessMethod WebAccessManager "http" #(@get:gethttp,@post:posthttp)
-
-
- --->>>
-